package com.itheima.stock.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.itheima.stock.api.StockControllerAPI;
import com.itheima.stock.dto.StockExportDTO;
import com.itheima.stock.entity.StockBusiness;

import com.itheima.stock.response.PageResult;
import com.itheima.stock.response.ResponseResult;
import com.itheima.stock.service.StockService;
import com.itheima.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.util.UriEncoder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zxh
 * @created 2024-10-30 19:41:03
 * @description
 */
@Slf4j
@RestController
public class StockController implements StockControllerAPI {

    private final StockService stockService;

    public StockController(StockService stockService) {
        this.stockService = stockService;
    }

    /**
     * 个股涨跌幅度区间统计
     * @return
     */
    @Override
    public ResponseResult<StockUpDownVO> stockUpDown() {
        return ResponseResult.ok(stockService.stockUpDown());
    }

    /**
     * 成交量对比
     */
    @Override
    public ResponseResult<TradeAmtVO<CountVo>> tradeAmt() {
        return ResponseResult.ok(stockService.tradeAmt());
    }

    /**
     * 导出
     * @param page
     * @param pageSize
     * @param response
     */
    @Override
    public void export(int page, int pageSize , HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            String filename = "stock";
            URLEncoder.encode(filename, StandardCharsets.UTF_8);
            response.setHeader("Content-Disposition", "attachment;filename="+filename+".xlsx");
            PageResult<MoreVO> more = stockService.more(page, pageSize);
            List<MoreVO> rows = more.getRows();
            List<StockExportDTO> list  = new ArrayList<>();
            long num = 1;
            for (MoreVO row : rows) {
                StockExportDTO dto = new StockExportDTO();
                BeanUtil.copyProperties(row, dto);
                dto.setNum(num);
                num++;
                list.add(dto);
            }
            EasyExcel.write(response.getOutputStream(),StockExportDTO.class).sheet("股票数据").doWrite(list);
        } catch (Exception e) {
            log.info("股票excel数据导出异常，当前页：{}，每页大小：{}，异常信息：{}",page,pageSize,e.getMessage());
        }


    }

    /**
     * 获取涨跌榜数据
     * @return ResponseResult<List<UpDownVO<CountVo>>>
     */
    @Override
    public ResponseResult<UpDownVO<CountVo>> UpDown() {
        UpDownVO<CountVo> upDown = stockService.UpDown();
        return ResponseResult.ok(upDown);
    }

    /**
     * 获取更多涨幅榜数据
     * @param page
     * @param pageSize
     */
    @Override
    public ResponseResult<PageResult<MoreVO>> more(Integer page, Integer pageSize) {
        PageResult<MoreVO> pageResult = stockService.more(page, pageSize);
        return ResponseResult.ok(pageResult);
    }

    /**
     * 股票涨幅榜数据
     * @return
     */
    @Override
    public ResponseResult<List<IncreaseVO>> increase() {
        List<IncreaseVO> increase = stockService.increase();
        return ResponseResult.ok(increase);
    }

    /**
     *获取股票主营业务
     * @return
     */
    @Override
    public ResponseResult<List<StockBusiness>> getStockBusiness() {
        List<StockBusiness> stockBusiness = stockService.getStockBusiness();
        return ResponseResult.ok(stockBusiness);
    }

    /**
     * 国内股票大盘指数
     * @return
     */
    @Override
    public ResponseResult<List<MarketIndexVO>> innerIndexAll() {
        List<MarketIndexVO> indexAll = stockService.innerIndexAll();
        return ResponseResult.ok(indexAll);
    }
    /**
     * 获取板块指数
     * @return
     */
    @Override
    public ResponseResult<List<SectorAllVO>> SectorAll() {
        List<SectorAllVO> sectorAll = stockService.SectorAll();
        return ResponseResult.ok(sectorAll);
    }
}